package com.zhc.dejingapi.controller;


import com.github.pagehelper.PageInfo;
import com.zhc.dejingapi.entity.query.CodeQuery;
import com.zhc.dejingapi.entity.vo.CodeVo;
import com.zhc.dejingapi.pojo.param.QingCommonBackResult;
import com.zhc.dejingapi.pojo.param.QingFcmoroutingMainParam;
import com.zhc.dejingapi.pojo.param.QingWaterboiltaskfinishParam;
import com.zhc.dejingapi.service.upload.IQingFcMoroutingMainReportService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 注塑压管报工表 前端控制器
 * </p>
 *
 * @author zhc
 * @since 2024-08-10
 */
@RestController
@RequestMapping("/qingfc")
@CrossOrigin
public class QingFcmoroutingmainController {

    private final IQingFcMoroutingMainReportService qingFcMoroutingMainReportService;
    private final JdbcTemplate mysqlJdbcTemplate;
    public QingFcmoroutingmainController(@Qualifier("mySqlJdbcTemplate")JdbcTemplate mysqlJdbcTemplate1,
                                         IQingFcMoroutingMainReportService qingFcMoroutingMainReportService1) {
        this.qingFcMoroutingMainReportService = qingFcMoroutingMainReportService1;
        this.mysqlJdbcTemplate = mysqlJdbcTemplate1;
    }

    @PostMapping("/reportJob")
    public QingCommonBackResult reportJob(@RequestHeader(name = "apiKey") String apiKey,
                                          @RequestBody QingFcmoroutingMainParam param){
        QingCommonBackResult commonBackResult=new QingCommonBackResult();
        try{
            if(param==null){
                throw new Exception("出错：参数不能为空！");
            }
            if(apiKey==null||apiKey.isEmpty()){
                throw new Exception("出错：apiKey不能为空！");
            }
            if (param.getAppKey()==null||param.getAppKey().isEmpty()){
                throw new Exception("出错：参数内的appKey不能为空！");
            }
            if (param.getZsSendWorkCode()==null||param.getZsSendWorkCode().isEmpty()){
                throw new Exception("出错：生产任务单号[workTaskCode]不能为空！");
            }

            Boolean bNeedProBatch=false;
            if(bNeedProBatch){

                String strSqlBatch="select Count(*) from qing_sfcmoroutingshift where qingRowCode=? and appKey=?";
                int iHaveCount=mysqlJdbcTemplate.queryForObject(strSqlBatch,Integer.class,param.getZsSendWorkCode(),param.getAppKey());

                if(iHaveCount<1){
                    throw new Exception("出错：当前生产任务单["+param.getZsSendWorkCode()+"]不存在或者未下载！");
                }

                if(param.getProcessPlanCode()==null||param.getProcessPlanCode().isEmpty()){
                    strSqlBatch="select moTaskCode from qing_sfcmoroutingshift where qingRowCode=? and appKey=?";
                    String strMoTaskCode=mysqlJdbcTemplate.queryForObject(strSqlBatch,String.class,param.getZsSendWorkCode(),param.getAppKey());
                    if(strMoTaskCode==null||strMoTaskCode.isEmpty()){
                        throw new Exception("出错：当前生产工单不存在或者未下载！");
                    }
                    param.setProcessPlanCode(strMoTaskCode);
                }

                strSqlBatch="select Count(*) from qing_moordermain where qingRowCode=?";

                iHaveCount=mysqlJdbcTemplate.queryForObject(strSqlBatch,Integer.class,param.getProcessPlanCode());
                if(iHaveCount<1){
                    throw new Exception("出错：当前生产工单["+param.getProcessPlanCode()+"]不存在或者未下载！");
                }
                String strSql="SELECT proBatchNo from qing_moordermain where qingRowCode=?";
                String strProBatchNo=mysqlJdbcTemplate.queryForObject(strSql,String.class,param.getProcessPlanCode());
                if(strProBatchNo==null||strProBatchNo.isEmpty()){
                    throw new Exception("出错：当前生产工单["+param.getProcessPlanCode()+"]批次号为空！");
                }
                SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
                String strDateNo=simpleDateFormat.format(new Date());
                strDateNo=strDateNo.substring(2);//yyMMdd

                if(strProBatchNo.length()>6){
                    strProBatchNo=strProBatchNo.substring(0,strProBatchNo.length()-6);
                    strProBatchNo=strProBatchNo+strDateNo;
                }
                else{
                    strProBatchNo=strDateNo;
                }
                param.setProBatchNo(strProBatchNo);
            }
            String strApiKey="B82ADA3F119D42A0B3AC237FDD6C40960A9065EB402A4D73B4D227AF15EF6D88";
            if(!apiKey.equals(strApiKey)){
                throw new Exception("出错：apiKey不正确！");
            }
            commonBackResult= qingFcMoroutingMainReportService.ReportJob(param);
        }
        catch (Exception e){
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());
        }

        return commonBackResult;
    }

    @PostMapping("/reportBoil")
    public QingCommonBackResult reportBoil(@RequestHeader(name = "apiKey") String apiKey,
                                          @RequestBody QingWaterboiltaskfinishParam param){
        QingCommonBackResult commonBackResult=new QingCommonBackResult();
        String strSql="";
        try{

            if(param==null){
                throw new Exception("出错：参数不能为空！");
            }
            if(apiKey==null||apiKey.isEmpty()){
                throw new Exception("出错：apiKey不能为空！");
            }


            if (param.getBoilStartRowNo()==null||param.getBoilStartRowNo().isEmpty()){
                throw new Exception("出错：水煮开始标识号[boilStartRowNo]不能为空！");
            }

            if (param.getZsSendWorkCode()==null||param.getZsSendWorkCode().isEmpty()){
                throw new Exception("出错：生产任务单号[workTaskCode]不能为空！");
            }

            if(param.getPsnCode()==null||param.getPsnCode().isEmpty()){
                throw new Exception("出错：员工工号[psnCode]不能为空！");
            }
            strSql="select Count(*) from qing_person where psnCode=?";
            int iHaveCount=mysqlJdbcTemplate.queryForObject(strSql,Integer.class,param.getPsnCode());
            if(iHaveCount<1){
                throw new Exception("出错：员工工号[psnCode]["+param.getPsnCode()+"]不存在！");
            }
            if (param.getPsnName()==null||param.getPsnName().isEmpty()){
                strSql="select psnName from qing_person where psnCode=?";
                String strpsnName=mysqlJdbcTemplate.queryForObject(strSql,String.class,param.getPsnCode());
                param.setPsnName(strpsnName);
            }
            String strApiKey="B82ADA3F119D42A0B3AC237FDD6C40960A9065EB402A4D73B4D227AF15EF6D88";

            if(!apiKey.equals(strApiKey)){
                throw new Exception("出错：apiKey不正确！");
            }
            commonBackResult= qingFcMoroutingMainReportService.ReportBoil(param);


        }
        catch (Exception e){
            e.printStackTrace();
            commonBackResult.setErrCode(-10012);
            commonBackResult.setErrMsg("出错："+e.getMessage()+"\n"+e.getStackTrace());

        }

        return commonBackResult;

    }

    @PostMapping("/getCodeList")
    private Map<Object, Object> getCodeList(@RequestBody CodeQuery codeQuery){
        PageInfo<CodeVo> page = qingFcMoroutingMainReportService.getCodeList(codeQuery);
        Map<Object, Object> map = new HashMap<>();
        map.put("records",page.getList());
        map.put("total",page.getTotal());
        return  map;
    }
}
